/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webcarrito.factory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author root
 */
public class Conexion {

    private static Connection conexion = null;
    private Context ctx = null;
    private javax.sql.DataSource ds = null;
    private String usuario = "";
    private String password = "";

    public Conexion(String usuario, String password) {
        this.usuario = usuario;
        this.password = password;
    }

    public java.sql.Connection get() {
        try {

            if (conexion == null || conexion.isClosed()) {
                try {
                    ctx = new InitialContext();
                    ds = (javax.sql.DataSource) ctx.lookup("jdbc/dbComprasDataSource");
                    this.conexion = ds.getConnection(this.usuario, password);
                } catch (NamingException ex) {
                    Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println("Conexion establecida con exito");
        return conexion;
    }
}
